<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * VoucherLunisolar Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class VoucherLunisolar extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('voucherLunisolar');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('voucherLunisolar_model');
			$this->set_controller('voucherLunisolar');
			$this->set_model($this->voucherLunisolar_model);
		}
                
                $this->load->model('booking_model');
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        private function updateTrade(){
            //update A
            
            $sender                     =   new stdClass();
            $sender->customerID         =   trim($this->input->post('customerID_send'));
            $sender->phone              =   trim($this->input->post('phone_send'));            
            $sender->airline            =   '';
            $sender->booking_place_code =   '';
            $sender->booking_types      =   '';
            $sender->createdTime        =   '';
            $sender->net                =   0;
            $sender->sale               =   0;
            $sender->net_fare           =   0;
            $sender->sale_fare          =   0;
            $sender->residual           =   trim($this->input->post('amount_send_fake'));
            $sender->checkbox_khautru   =   NULL;
            $sender->checkbox_sodukh    =   1;
            $sender->note_bk            =   '';
            $sender->note_pay           =   '[VTCND] Thêm';
            $sender->payment_method     =   0;
            $sender->payment_confirm    =   0;
            $sender->owner              =   trim($this->input->post('booker_send'));
            $sender->agent_code         =   $this->session->userdata['agent_code'];
            $sender->lastupdated        =   date("Y-m-d H:i:s",time());
            
            //update tat ca kh cua owner chuyen so du ve = 0 het.
            $this->voucherLunisolar_model->updateResidual($sender);
            $id1 = $this->voucherLunisolar_model->create($sender);
            
            $sender->customerID         =   trim($this->input->post('customerID_recieve'));
            $sender->phone              =   trim($this->input->post('phone_recieve'));
            $sender->residual           =   trim($this->input->post('amount_recieve_fake'));
            $sender->owner              =   trim($this->input->post('booker_recieve'));
            //update B
            $id2 = $this->voucherLunisolar_model->create($sender);
            if($id1 && $id2){
                $notice                 =   $this->lang->line('update_successful');
                $logAction              =   "[AddCNDSuccess]--".  json_encode($sender);
            } else {
                $notice                 =   $this->lang->line('error');
                $logAction              =   "[AddCNDError]--".  json_encode($sender);
            }
            $this->session->set_flashdata('flash_message', $notice);
            $paramAdminLog  = array(
                'userid'            => $this->session->userdata['user_id'],
                'lastLogin'         => date('Y-m-d :H:i:s',time()),
                'ip'                => $_SERVER['REMOTE_ADDR'],
                'logAction'         => $logAction,
                'agent_code'        => $this->session->userdata['agent_code']
            );
            $this->user_model->insertUserAdminLog($paramAdminLog);
            echo '<script>window.top.location.href="/auth/voucherLunisolar/";</script>';exit; 
        }

        public function trade(){
            
                if($this->input->server('REQUEST_METHOD')=='POST'){
                    $this->updateTrade();
                }
                
                if($this->input->get("id")){
                    $info   =   $this->voucherLunisolar_model->find_by(array('id'=>$this->input->get("id")));
                    $infos   =   $this->voucherLunisolar_model->find_by(array('customerID'=>$info[0]->customerID,'owner'=>$info[0]->owner,'checkbox_sodukh'=>1),'residual');
                    $sum = 0;
                    foreach($infos as $values){
                        $sum += $values->residual;
                    }
                    
                    $data                   =   new stdClass();
                    $data->phone_send       =   $info[0]->phone;
                    $data->customerID_send  =   $info[0]->customerID;
                    $data->amount_send      =   $sum;
                    $data->owner            =   $info[0]->owner;
                    
                    $booker     =   $this->user_model->find_by(array('group_id'=>78,'active'=>1),"id,firstname");
                    $user_info[] = '--Choosen--';
                    foreach($booker as $ks => $vs){
                        $user_info[$vs->id]             =   $vs->firstname;
                    }

                    $this->view_data['info']                =   $data;
                    $this->view_data['booker']              =   $user_info;
                    $this->load->view('auth/voucherLunisolar/trade', $this->view_data);
                }
        }
}